package Gui;

import java.awt.GraphicsEnvironment;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;

public class MyOptionBar extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextPane textPane;
	private static final Integer startFontSize = 6;
	private static final Integer endFontSize = 50;

	public MyOptionBar(JTextPane tp, TextFieldHandler th, ForceUpdateHandler uh){
		setTextPane(tp);
		init(th,uh);
	}
	
	private void init(TextFieldHandler th, ForceUpdateHandler uh){
		FontActions fontActions = new FontActions(getTextPane());
		
		//font for the buttons themself
		JToggleButton bold = new JToggleButton("<html><b>B</b></html>");
		FontActions.MyBoldAction boldAction = fontActions.new MyBoldAction();
		bold.addActionListener(boldAction);
		add(bold);
		
		JToggleButton italic = new JToggleButton("<html><i>I</i></html>");
		FontActions.MyItalicAction italicAction = fontActions.new MyItalicAction();
		italic.addActionListener(italicAction);
		add(italic);
		
		JToggleButton underline = new JToggleButton("<html><u>U</u></html>");
		FontActions.MyUnderlineAction underlineAction = fontActions.new MyUnderlineAction();
		underline.addActionListener(underlineAction);
		add(underline);
		
		//font family
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    String[] fontNames = ge.getAvailableFontFamilyNames();

	    JComboBox<String> fontFamilyChooser = new JComboBox<String>(fontNames);
	    //set correct family in combobox
	    fontFamilyChooser.setSelectedItem(getTextPane().getFont().getFamily());	    
	    //setting up the listener
	    fontFamilyChooser.addActionListener(new FontFamilyListener(fontActions));
	    add(fontFamilyChooser);	    
	    
	    //font size
	    JComboBox<Integer> fontSizeChooser = new JComboBox<Integer>();
	    //editable does not seem to work
	    //fontSizeChooser.setEditable(true);
	    for (int i = startFontSize; i < endFontSize ; i+=2) {
		      fontSizeChooser.addItem(i);
		}
	    //set correct size in combobox
	    fontSizeChooser.setSelectedItem(getTextPane().getFont().getSize());	
	    //set up listener
	    fontSizeChooser.addActionListener(new FontSizeListener(fontActions));
	    add(fontSizeChooser);	 
	    
	    //update/commit buttons
	    JButton commit = new JButton("Commit");
	    commit.addActionListener(th);
	    
	    JButton update = new JButton("Update");
	    update.addActionListener(uh);
	    
	    add(commit);
	    add(update);
	    
	}

	public JTextPane getTextPane() {
		return textPane;
	}

	public void setTextPane(JTextPane textPane) {
		this.textPane = textPane;
	}

}
